package org.x.mq.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import org.springframework.amqp.rabbit.annotation.EnableRabbit;

import static org.x.mq.config.MQConstant.*;

@Configuration
@EnableRabbit
public class RabbitmqConfig {

    // ==================== 简单模式配置 ====================
    @Bean(QUEUE_SIMPLE)
    public Queue QUEUE_SIMPLE() {
        return new Queue(QUEUE_SIMPLE);
    }

    // ==================== 工作队列模式配置 ====================
    @Bean(QUEUE_WORK)
    public Queue QUEUE_WORK() {
        return new Queue(QUEUE_WORK);
    }

    // ==================== 发布订阅模式配置 ====================
    @Bean(EXCHANGE_FANOUT)
    public FanoutExchange EXCHANGE_FANOUT() {
        return ExchangeBuilder.fanoutExchange(EXCHANGE_FANOUT).durable(true).build();
    }

    @Bean(QUEUE_FANOUT_1)
    public Queue QUEUE_FANOUT_1() {
        return new Queue(QUEUE_FANOUT_1);
    }

    @Bean(QUEUE_FANOUT_2)
    public Queue QUEUE_FANOUT_2() {
        return new Queue(QUEUE_FANOUT_2);
    }

    @Bean
    public Binding BINDING_QUEUE_FANOUT_1(@Qualifier(QUEUE_FANOUT_1) Queue queue,
                                          @Qualifier(EXCHANGE_FANOUT) FanoutExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange);
    }

    @Bean
    public Binding BINDING_QUEUE_FANOUT_2(@Qualifier(QUEUE_FANOUT_2) Queue queue,
                                          @Qualifier(EXCHANGE_FANOUT) FanoutExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange);
    }

    // ==================== 路由模式配置 ====================
    @Bean(EXCHANGE_DIRECT)
    public DirectExchange EXCHANGE_DIRECT() {
        return ExchangeBuilder.directExchange(EXCHANGE_DIRECT).durable(true).build();
    }

    @Bean(QUEUE_DIRECT_ERROR)
    public Queue QUEUE_DIRECT_ERROR() {
        return new Queue(QUEUE_DIRECT_ERROR);
    }

    @Bean(QUEUE_DIRECT_INFO)
    public Queue QUEUE_DIRECT_INFO() {
        return new Queue(QUEUE_DIRECT_INFO);
    }

    @Bean
    public Binding BINDING_DIRECT_ERROR(@Qualifier(QUEUE_DIRECT_ERROR) Queue queue,
                                        @Qualifier(EXCHANGE_DIRECT) DirectExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(ROUTINGKEY_ERROR);
    }

    @Bean
    public Binding BINDING_DIRECT_INFO(@Qualifier(QUEUE_DIRECT_INFO) Queue queue,
                                       @Qualifier(EXCHANGE_DIRECT) DirectExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(ROUTINGKEY_INFO);
    }

    // ==================== 主题模式配置 ====================
    @Bean(EXCHANGE_TOPICS_INFORM)
    public TopicExchange EXCHANGE_TOPICS_INFORM() {
        return ExchangeBuilder.topicExchange(EXCHANGE_TOPICS_INFORM).durable(true).build();
    }

    @Bean(QUEUE_INFORM_EMAIL)
    public Queue QUEUE_INFORM_EMAIL() {
        return new Queue(QUEUE_INFORM_EMAIL);
    }

    @Bean(QUEUE_INFORM_SMS)
    public Queue QUEUE_INFORM_SMS() {
        return new Queue(QUEUE_INFORM_SMS);
    }

    @Bean
    public Binding BINDING_QUEUE_INFORM_EMAIL(@Qualifier(QUEUE_INFORM_EMAIL) Queue queue,
                                              @Qualifier(EXCHANGE_TOPICS_INFORM) TopicExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(ROUTINGKEY_EMAIL);
    }

    @Bean
    public Binding BINDING_QUEUE_INFORM_SMS(@Qualifier(QUEUE_INFORM_SMS) Queue queue,
                                            @Qualifier(EXCHANGE_TOPICS_INFORM) TopicExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(ROUTINGKEY_SMS);
    }
}